from django.db import IntegrityError
from django.shortcuts import render
from rest_framework.request import Request
from rest_framework.response import Response
from rest_framework.views import APIView

from plugins.viewset.Mixin import CacheModelSet
from user.models import User
from user.serializers import UserSerializer


# Create your views here.

class Login(APIView):
    def post(self, request: Request):
        params: dict = request.data
        username = params.get("username")
        password = params.get("password")
        try:
            User.objects.create_user(username, password=password)
            return Response("成功")
        except IntegrityError as e:
            raise e


class TestUser(CacheModelSet):
    queryset = User.objects.all()
    serializer_class = UserSerializer
    cache_key_template = "user_{handler}"
    expires_during = [10, 20]
